package cn.wgx.module.mind.service;

import cn.wgx.common.entity.mind.*;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.module.mind.entity.mapper.ProjectMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;

@Service
public class ProjectService extends BaseMindService<ProjectMapper, MindProject> {

    @Autowired
    private ProjectRoleService projectRoleService;
    @Autowired
    private ProjectUserRoleService projectUserRoleService;
    @Autowired
    private ProjectUserService projectUserService;

    @Autowired
    private ChannelService channelService;
    @Autowired
    private BlockService blockService;
    @Autowired
    private GroupService groupService;
    @Autowired
    private BeatService beatService;

    @Autowired
    private ModService modService;
    @Autowired
    private DivideService divideService;
    @Autowired
    private HeadService headService;
    @Autowired
    private LinkService linkService;


    @Override
    public <E extends IPage<MindProject>> E page(E page, Wrapper<MindProject> queryWrapper) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        if (loginAppUser == null) {
            return page;
        }
        Integer user_id = loginAppUser.getUser_id();
        QueryWrapper<MindProject> queryWrapper1 = (QueryWrapper<MindProject>) queryWrapper;
        queryWrapper1.and(i -> i.inSql("pro_id",
                "select pro_id from mind_pro_user where user_id=" + user_id)
                .or().eq("createUserId", user_id))
                .orderByDesc("createDate");
        return getBaseMapper().selectPage(page, queryWrapper);
    }


    @Override
    @Transactional
    public boolean save(MindProject entity) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        if (loginAppUser == null) {
            return false;
        }
//        entity.setCreateUserId(loginAppUser.getUser_id());
        boolean b = super.save(entity);
        if (b) {
            iniProject(entity);
        }
        return b;
    }

    @Override
    public boolean removeById(Serializable id) {
        boolean remove = super.removeById(id);
        if (remove) {
            destroyProject(id);
        }
        return false;
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        boolean remove = super.removeByIds(idList);
        if (remove) {
            for (Serializable id : idList) {
                destroyProject(id);
            }
        }
        return false;
    }

    /**
     * 项目创建初始化相关内容
     * 1. 创建角色-管理员
     * 2. 添加创建人员进入管理员组
     * 3. 管理员不可删除,只能在销毁项目的时候删除
     *
     * @param project
     * @return
     */
    public boolean iniProject(MindProject project) {
        //创建项目创建人关联
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        Integer user_id = loginAppUser.getUser_id();
        Integer pro_id = project.getPro_id();
        ProjectRole projectRole = new ProjectRole();
        projectRole.setPro_id(pro_id);
        projectRole.setRoleName("管理员");
        projectRole.setDes("管理员组");
        projectRole.setEName("admin");
        projectRole.setP_look(true);
        projectRole.setP_modify(true);
        projectRole.setP_del(true);
        projectRole.setP_add(true);
        projectRoleService.save(projectRole);

        ProjectRole projectRole2 = new ProjectRole();
        projectRole2.setPro_id(pro_id);
        projectRole2.setRoleName("用户");
        projectRole2.setDes("用户");
        projectRole2.setEName("user");
        projectRole2.setP_look(true);
        projectRole2.setP_modify(true);
        projectRole2.setP_del(true);
        projectRole2.setP_add(true);
        projectRoleService.save(projectRole2);
        //添加创建人为本项目管理员
        ProjectUserRole p = new ProjectUserRole();
        p.setPro_id(project.getPro_id());
        p.setUser_id(user_id);
        p.setPr_id(projectRole.getPr_id());
        projectUserRoleService.save(p);

        ProjectUser projectUser = new ProjectUser();
        projectUser.setUser_id(user_id);
        projectUser.setPro_id(pro_id);
        projectUser.setPro_role_id(projectRole.getPr_id());
        projectUserService.save(projectUser);

        //添加 角色\场景\道具
        MindMod mindMod = new MindMod();
        mindMod.setPro_id(pro_id);
        mindMod.setTitle("角色");
        modService.save(mindMod);
        mindMod.setTitle("场景");
        modService.save(mindMod);
        mindMod.setTitle("道具");
        modService.save(mindMod);

        //添加主通道
        MindChannel channel = new MindChannel();
        channel.setTitle("主通道");
        channel.setType("2");
        channel.setColor("#dbdbdb");
        channel.setPro_id(pro_id);
        channelService.save(channel);

        MindBeat mindBeat = new MindBeat();
        mindBeat.setPro_id(pro_id);
        mindBeat.setChannel_id(channel.getChannel_id());
        mindBeat.setTitle("节点1");
        mindBeat.setColor("#3674B3");
        mindBeat.setX(300d);
        mindBeat.setY(250d);
        beatService.save(mindBeat);

        return true;
    }

    /**
     * 删除项目需要销毁相关内容
     * 1. 角色
     * 2. 人员
     * 3. 内容数据
     *
     * @param pro_id
     * @return
     */
    public boolean destroyProject(Serializable pro_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_id", pro_id);
        //权限角色
        projectRoleService.remove(queryWrapper);
        projectUserRoleService.remove(queryWrapper);
        projectUserService.remove(queryWrapper);
        //数据
        beatService.remove(queryWrapper);
        beatService.remove(queryWrapper);
        channelService.remove(queryWrapper);
        modService.remove(queryWrapper);
        blockService.remove(queryWrapper);
        divideService.remove(queryWrapper);
        groupService.remove(queryWrapper);
        headService.remove(queryWrapper);
        linkService.remove(queryWrapper);
        return true;
    }
}
